package com.hndzyu.yd.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.Util;
import com.tencent.mm.opensdk.utils.Log;


import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by YJ on 2018/11/6.
 */
public class CodePopupWindow extends PopupWindow {
    @BindView(R.id.pop_close)
    ImageView popClose;
    private View contextView;
    private Activity mContext;
    @BindView(R.id.iv_head)
    CircleImageView ivHead;
    @BindView(R.id.tv_nc)
    TextView tvNc;
    @BindView(R.id.userid)
    TextView userid;
    @BindView(R.id.iv_ewm)
    ImageView ivEwm;

    public CodePopupWindow(Activity context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contextView = inflater.inflate(R.layout.popup_code, null);
        mContext = context;
        this.setContentView(contextView);
        ButterKnife.bind(this, contextView);
        userid.setText("  ID:" + UserBean.uid + "  ");
        tvNc.setText("".equals(UserBean.nickname) ? "赚小宝" : UserBean.nickname);
        Glide.with(context).load(Path.BASE_URL + UserBean.headimg)
                .apply(new RequestOptions().error(R.mipmap.head)).into(ivHead);
        ivEwm.setImageBitmap(Util.encodeAsBitmap2(Path.zxb_register(UserBean.ip,UserBean.uid)));
        Log.i("_______",UserBean.qudao);
        popClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        this.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(30000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.my_dialog);
    }

    public void showDown(View view) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 0.7f;
        mContext.getWindow().setAttributes(lp);
        this.showAtLocation(view, Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 1f;
        mContext.getWindow().setAttributes(lp);
    }
}
